<%@ include file="/en/jsp/static.jsp" %>
<%@ page import="java.util.*,linuxbox.BoxStatic" %>
<%@ taglib uri="/linuxbox/common.tld" prefix="common" %>

<%
  int MONTH = -1;
  int YEAR = -1;
  String PREFIX = "";
  String str = (String) request.getParameter("ITEMPREFIX");
  if ( str != null )
  {
    PREFIX = str;
  }
  str = (String) request.getParameter("MONTH");
  if ( str != null )
  {
    MONTH = Integer.parseInt(str);
  }
  str = (String) request.getParameter("YEAR");
  if ( str != null )
  {
    YEAR = Integer.parseInt(str);
  }
  Calendar currentDate = Calendar.getInstance(sec.getTimeZone());
  if ( MONTH != -1 && YEAR != -1 )
  {
    currentDate.set(YEAR, MONTH,1);
  }
  else
  {
    MONTH = currentDate.get(Calendar.MONTH);
    YEAR = currentDate.get(Calendar.YEAR);
  }

  Calendar start = Calendar.getInstance(sec.getTimeZone());
  Calendar end = Calendar.getInstance(sec.getTimeZone());

  start.setTime(currentDate.getTime());
  end.setTime(start.getTime());

  start.set(Calendar.DATE,1);
  int dayOfWeek = start.get(Calendar.DAY_OF_WEEK)-1;
  start.add(Calendar.DAY_OF_YEAR,-dayOfWeek);

  end.add(Calendar.MONTH, 1);
  end.set(Calendar.DATE,1);
  end.add(Calendar.DATE,-1);
  dayOfWeek = end.get(Calendar.DAY_OF_WEEK);
  end.add(Calendar.DAY_OF_YEAR,(7-dayOfWeek));
%>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--
function SwapDate(monVal, dayVal, yearVal)
{
  if ( opener != null )
  {
    opener.document.theForm.<%=PREFIX%>_DAY.selectedIndex = dayVal - 1;
    opener.document.theForm.<%=PREFIX%>_MONTH.selectedIndex = monVal;

    var l = opener.document.theForm.<%=PREFIX%>_YEAR.options.length;
    for (var i = 0; i < l; i++)
    {
      if (window.opener.document.theForm.<%=PREFIX%>_YEAR.options[i].value == yearVal)
      {
        window.opener.document.theForm.<%=PREFIX%>_YEAR.options.selectedIndex = i;
        break;
      }
    }
  }
  window.close();
}
// -->
</SCRIPT>

<SCRIPT LANGUAGE="JavaScript" TYPE="text/javascript">
<!--

function ReopenDateSelector()
{
//	var filename = ('<%=ROOT%>/jsp/calendar/BoxPopupDayPicker.jsp?MONTH=' + dateMonth + '&YEAR=' + dateYear);
  var year = document.DayPicker.SELECT_YEAR.options[document.DayPicker.SELECT_YEAR.selectedIndex].value;
  var month = document.DayPicker.SELECT_MONTH.options[document.DayPicker.SELECT_MONTH.selectedIndex].value;
  window.document.location.href = '<%=BoxStatic.jspDir%>calendar/BoxPopupDayPicker.jsp?MONTH='+month+'&YEAR='+year+'&ITEMPREFIX='+'<%=PREFIX%>';
}

// -->
</SCRIPT><!-- jscript for  date picker -->

<html>
<head>
</head>
<body>
<form name=DayPicker>
<table>
  <tr  bgcolor=9bbad6>
  <td ALIGN="center">
    <table CELLSPACING="0" CELLPADDING="1" BORDER="0">
      <tr >
        <td ALIGN="left" width="1%" >
          <a href="<%=BoxStatic.jspDir%>calendar/BoxPopupDayPicker.jsp?<%
                  if ( MONTH == Calendar.JANUARY )
                    out.print("MONTH="+Calendar.DECEMBER+"&YEAR="+(YEAR-1));
                  else
                    out.print("MONTH="+(MONTH-1)+"&YEAR="+YEAR);
                  out.print("&ITEMPREFIX="+PREFIX);
              %>">
              <img SRC="<%=BoxStatic.imageDir%>cal_left.gif" WIDTH="11" HEIGHT="11" BORDER="0">
          </a>
        </td>
        <td NOWRAP VALIGN="center" width="1%">
          <tt>
            <b>
			      	 <%=StringUtil.getDateTimeFormatString(sec.getTimeZone(), BoxStatic.MONTH_YEAR_FORMAT, currentDate.getTime())%>
            </b>
          </tt>
        </td>
        <td ALIGN="right" width="1%" >
          <a href="<%=BoxStatic.jspDir%>calendar/BoxPopupDayPicker.jsp?<%
                  if ( MONTH == Calendar.DECEMBER )
                    out.print("MONTH="+Calendar.JANUARY+"&YEAR="+(YEAR+1));
                  else
                    out.print("MONTH="+(MONTH+1)+"&YEAR="+YEAR);
                  out.print("&ITEMPREFIX="+PREFIX);
          %>">
          <img SRC="<%=BoxStatic.imageDir%>cal_right.gif" WIDTH="11" HEIGHT="11" BORDER="0"></a>
        </td>
      </tr>
    </table>
  </td>
  </tr>
  <tr>
  <td>
    <table BGCOLOR="#ffffff" CELLSPACING="3" CELLPADDING="1" BORDER="0">
      <tr>
    <%
        Calendar tmp = Calendar.getInstance(sec.getTimeZone());
        tmp.setTime(start.getTime());
        long endTime = end.getTime().getTime();
        for(int i = 1; i <= 7; i++)
        {
    %>
          <td valign="top" align="right">
          <font size="-1"><%=BoxStatic.shortWeekSymbols[i].substring(0,2)%><br>
    <%
          while(tmp.getTime().getTime() <= endTime)
          {
            int tmpMon = tmp.get(Calendar.MONTH);
            int tmpYear = tmp.get(Calendar.YEAR);
            int tmpDay = tmp.get(Calendar.DATE);

            boolean sameMonth = (tmpMon == MONTH);
//            out.print("<a href=\""+ROOT+"/BoxCalendarAction?v=day&t="+tmp.getTime().getTime()+"\">");

            out.print("<A HREF=\"#\" ONCLICK=\"SwapDate("+tmpMon+","+tmpDay+","+tmpYear+")\">");
            if ( !sameMonth )
              out.print("<font color=\"#999999\">");
            out.print(tmpDay);
            if ( !sameMonth )
              out.print("</font>");
            out.print("</a><br>");
            tmp.add(Calendar.DAY_OF_YEAR,7);
          } //end while
          out.print("</font> </td>");

          tmp.setTime(start.getTime());
          tmp.add(Calendar.DAY_OF_YEAR,i);
        } //end for
    %>
      </tr>
    </table>
  </td>
  </tr>
  <tr>
    <td ALIGN="center">
      <select name="SELECT_MONTH" size="1" onChange="ReopenDateSelector()">
        <common:MonthList selected="<%=MONTH%>" allownull="false" />
      </select>
      <select name="SELECT_YEAR" size="1" onChange="ReopenDateSelector()">
        <common:YearList selected="<%=YEAR%>" allownull="false" />
      </select>
    </td>
  </tr>
</table>
</form>
</body>
</html>
